// pages/newcamera/newcamera.js
const ctxPuzzle = wx.createCanvasContext("puzzleCanvas", this)
Page({
  data:{
    userId:null,
    familyId:null,
    userType:2,
    cameraInfo: {
      width: 0,
      height:0,
    },
    systemInfo:{},
    frameImageInfo:{},
    devicePosition:'front'
  },
  onLoad(options) {
    this.setData({
      userId:options.userId,
      familyId:options.familyId,
      userType:options.userType
    })
    var that = this
    that.ctx = wx.createCameraContext()
    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          menuButtonBoundingClientRect: wx.getMenuButtonBoundingClientRect(),
          systemInfo:res
        })
        wx.getImageInfo({
          src: '../../images/xiangkuang.png',
          success:res=>{
            console.log(res)
            console.log(that.data.systemInfo)
            var cameraInfo = {}
            cameraInfo.width = that.data.systemInfo.windowWidth
            cameraInfo.height = that.data.systemInfo.windowWidth / res.width * res.height
            cameraInfo.top = (that.data.systemInfo.windowHeight-cameraInfo.height)/2
            that.setData({
              cameraInfo:cameraInfo,
              frameImageInfo:res
            })
            console.log(that.data.cameraInfo)
          }
        })
      },
    })
  },
  takePhoto() {
    wx.showNavigationBarLoading() //在当前页面显示导航条加载动画
    wx.showLoading({ //显示 loading 提示框
      title: "正在拍照...",
    })
    var that = this
    that.ctx.takePhoto({
      quality: 'low',
      success: (res) => {
        wx.hideNavigationBarLoading() //在当前页面隐藏导航条加载动画
        wx.hideLoading() //隐藏 loading 提示框
        wx.redirectTo({
          url: '../preview/preview?src=' + res.tempImagePath+ '&userType=' + that.data.userType + '&userId=' + that.data.userId + '&familyId=' + that.data.familyId,
        })
        // wx.getImageInfo({
        //   src: res.tempImagePath,
        //   success:res=>{
        //     console.log(res)
           
            // ctxPuzzle.drawImage(res.path, 0, res.height*that.data.cameraInfo.top/that.data.systemInfo.windowHeight, res.width, res.height*that.data.cameraInfo.height/that.data.systemInfo.windowHeight, 0, 0, that.data.cameraInfo.width, that.data.cameraInfo.height)
            // ctxPuzzle.drawImage(that.data.frameImageInfo.path, 0, 0, that.data.frameImageInfo.width, that.data.frameImageInfo.height,0,0,that.data.cameraInfo.width,that.data.cameraInfo.height)
            // ctxPuzzle.draw()
            // var timeName = setTimeout(function () {
            //   wx.canvasToTempFilePath({
            //     canvasId: 'puzzleCanvas',
            //     fileType: "png",
            //     success: function (res) {
            //       wx.hideNavigationBarLoading() //在当前页面隐藏导航条加载动画
            //       wx.hideLoading() //隐藏 loading 提示框
            //       wx.redirectTo({
            //         url: '../preview/preview?src=' + res.tempFilePath + '&userType=' + that.data.userType + '&userId=' + that.data.userId + '&familyId=' + that.data.familyId,
            //       })
            //     }
            //   })
            // }, 1000)
        //   }
        // })
      }
    })
  },
  switchCamera(){
    if(this.data.devicePosition=="back"){
      this.setData({
        devicePosition:"front"
      })
    }else{
      this.setData({
        devicePosition: 'back'
      })
    }
  },
  startRecord() {
    this.ctx.startRecord({
      success: (res) => {
        console.log('startRecord')
      }
    })
  },
  stopRecord() {
    this.ctx.stopRecord({
      success: (res) => {
        this.setData({
          src: res.tempThumbPath,
          videoSrc: res.tempVideoPath
        })
      }
    })
  },
  error(e) {
    console.log(e.detail)
  },

  go2Home() {
    wx.navigateBack({
      delta: 1,
    })
  },
})